package org.ccay.excel.importer.validate.impl;

import org.ccay.core.util.StringUtil;
import org.ccay.excel.importer.validate.AbstractValidator;
import org.ccay.excel.importer.validate.IStatus;

public class IntegerRangeValidator extends AbstractValidator{

	/**
	 * 整数长度校验器。
	 * 	
	 * @author chaowangbang
	 * @since 2012-12-25
	 */
	
	public static final String INTEGER_MESSAGE_KEY = "ccay.excel.validate.integerRange.error";
	
	/**
	 * 最小值
	 */
	private int minValue = Integer.MIN_VALUE;
	
	/**
	 * 最大值
	 */
	private int maxValue = Integer.MAX_VALUE;

	
	public int getMinValue() {
		return minValue;
	}


	public void setMinValue(int minValue) {
		this.minValue = minValue;
	}


	public int getMaxValue() {
		return maxValue;
	}


	public void setMaxValue(int maxValue) {
		this.maxValue = maxValue;
	}


	public IStatus doValidate(Object value) {
		if(null == value || StringUtil.isNullOrEmpty(value.toString())) {
			return IStatus.Ok;	
		}
		try{
			int number = Integer.parseInt(value.toString());
			if(number < minValue || number > maxValue){
				super.setMessageKey(INTEGER_MESSAGE_KEY);
				return error();
			}
		}catch(NumberFormatException ex){
			return IStatus.Ok;	
		}
		return IStatus.Ok;	
	}
}
